<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/6/13
 * Time: 13:47
 */

namespace App\Http\Controllers\api\news;


use App\Http\Controllers\api\base\BaseController;
use App\Http\Resources\goods\GoodsEvaluatesPageCollection;
use App\Http\Resources\news\NewsCommentResource;
use App\Http\Resources\news\NewsResource;
use App\Http\Resources\news\NoticeResource;
use App\Models\news\Comment;
use App\Models\news\News;
use App\Models\news\NewsCate;
use App\Models\news\Notice;
use App\Models\user\Collect;
use Illuminate\Http\Request;

class NewsController extends BaseController
{

    /**
     * 获取新闻分类
     * @param Request $request
     * @return mixed
     */
    public function newCate(Request $request)
    {
        $map = [];
        if($request->pid){
            $map['pid'] = $request->pid;
        }
        $map['status'] = 1;
        $data = NewsCate::where($map)->get();
        return success($data);
    }

    /**
     * 资讯列表
     * @param Request $request
     * @return mixed
     */
    public function getNewsList(Request $request)
    {
        if($request->cate_id){
            $map['cate_id'] = $request->cate_id;
        }
        $pageSize = $request->page_size;
        if(!$pageSize || !is_numeric($pageSize)){
            $pageSize = 10;
        }
        $hot = $request->hot;
        if(is_numeric($hot)){
            $map['recommend'] = $hot;
        }
        $map['status'] = 1;
        $data = News::query()->where($map)->orderByDesc('publish_time')->paginate($pageSize);
        foreach ($data as &$item){
            $item['comment_number'] = Comment::where('news_id',$item['resource_id'])->count();
        }
        return success( new NewsResource( $data) );
    }

    /**
     * 资讯详情
     * @param Request $request
     * @return mixed
     */
    public function getNewsDetail(Request $request)
    {
        $id = $request->id;
        if(!$id){
            return error(205);
        }
        $map['id'] = $id;
        $data = News::query()->where($map)->first();
        if(!$data){
            return error(204);
        }
        $where['resource_id'] = $data['id'];
        $where['user_id'] = $this->user_id;
        $data['collect_status'] = Collect::where($where)->value('status') ? 1 : 0;
        return success($data);
    }

    /**
     * 评论资讯
     * @param Request $request
     */
    public function setComment(Request $request)
    {
        $news_id = $request->news_id;
        if(!$news_id || !is_numeric($news_id)){
            return error(205);
        }
        $data['news_id'] = $news_id;
        $data['content'] = $request->comment_content;
        $data['user_id'] = $this->user_id;
        $data = Comment::query()->firstOrCreate($data);
        return success($data);
    }

    /**
     * 资讯评论列表
     * @param Request $request
     * @return mixed
     */
    public function getComment(Request $request)
    {
        $news_id = $request->news_id;
        if(!$news_id || !is_numeric($news_id)){
            return error(205);
        }
        $pageSize = $request->page_size;
        if(!$pageSize || !is_numeric($pageSize)){
            $pageSize = 10;
        }
        $commentMap = [
            'news_id' => $news_id,
            'status' => 1
        ];
        $data = Comment::where($commentMap)->with('user')->orderByDesc('id')->paginate($pageSize);
        return success(new NewsCommentResource($data));
    }

    /**
     * 公告列表
     * @return mixed
     */
    public function getNotice(Request $request)
    {
        $pageSize = $request->page_size;
        if(!$pageSize || !is_numeric($pageSize)){
            $pageSize = 10;
        }
        if($request->type == 1){
            $map['recommend'] = 1;
        }
        $map['status'] = 1;
        $data = Notice::query()->where($map)->orderByDesc('id')->paginate($pageSize);
        return success(new NoticeResource($data));
    }

    /**
     * 公告详情
     * @param Request $request
     * @return mixed
     */
    public function getNoticeDetail(Request $request)
    {
        $id = $request->id;
        if(!$id){
            return error(205);
        }
        $map['id'] = $id;
        $data = Notice::query()->where($map)->first();
        return success($data);
    }
}